package com.test.controller;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.test.R;

/**
 * 多选则与按钮
 */
public class CheckedBoxActivity extends AppCompatActivity implements
        View.OnClickListener, CompoundButton.OnCheckedChangeListener {


    private CheckBox checkBoxOne;
    private CheckBox checkBoxTwo;
    private CheckBox checkBoxThree;
    private CheckBox checkBoxFour;

    private Button btnSend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //  setContentView(R.layout.activity_main);
        setContentView(R.layout.button_two);
        checkBoxOne = findViewById(R.id.checkBoxOne);
        checkBoxTwo = findViewById(R.id.checkBoxTwo);
        checkBoxThree = findViewById(R.id.checkBoxThree);
        checkBoxFour = findViewById(R.id.checkBoxFour);
        btnSend = findViewById(R.id.btnSend);

        checkBoxOne.setOnCheckedChangeListener(this);
        checkBoxTwo.setOnCheckedChangeListener(this);
        checkBoxThree.setOnCheckedChangeListener(this);
        checkBoxFour.setOnCheckedChangeListener(this);

        btnSend.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {

        String string = "";
        if (checkBoxOne.isChecked()) {
            string += checkBoxOne.getText().toString() + " ";
        }
        if (checkBoxTwo.isChecked()) {
            string += checkBoxTwo.getText().toString() + " ";
        }
        if (checkBoxThree.isChecked()) {
            string += checkBoxThree.getText().toString() + " ";
        }
        if (checkBoxFour.isChecked()) {
            string += checkBoxFour.getText().toString() + " ";
        }

        Toast.makeText(this, string, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String string = compoundButton.getText().toString();
        if (compoundButton.isChecked()) {
            Toast.makeText(this, "你选择的是：" + string, Toast.LENGTH_LONG).show();
        }
    }
}
